package com.situ.student.ui;

import java.awt.GridLayout;
import java.awt.JobAttributes;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Period;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.situ.student.util.JDBCUtil;

public class App {
	public static void main(String[] args) {
		JFrame jFrame = new JFrame();
		JPanel jPanel = new JPanel();
		jFrame.add(jPanel);
		JDBCUtil jdbcUtil = new JDBCUtil();
		// 布局 三行一列
		jPanel.setLayout(new GridLayout(3, 1));
		// 第一行 用户名
		JPanel one = new JPanel();
		JLabel jLabel = new JLabel("用户名:");
		JTextField jTextField = new JTextField(8);
		one.add(jLabel);
		one.add(jTextField);
		jPanel.add(one);
		// 第二行 密码
		JPanel two = new JPanel();
		JLabel password = new JLabel("密码 :");
		JTextField jTextField1 = new JTextField(8);
		two.add(password);
		two.add(jTextField1);
		jPanel.add(two);
		// 第三行 两个按钮
		JPanel three = new JPanel();
		JButton jButton = new JButton("确定");
		JButton jButton1 = new JButton("注册");
		three.add(jButton);
		three.add(jButton1);
		jPanel.add(three);
		// 事件监听 注册按钮
		jButton1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String username = jTextField.getText();
				String userpassword = jTextField1.getText();
				System.out.println(username);
				System.out.println(userpassword);
				Connection connection = null;
				PreparedStatement preparedStatement = null;
				int count=0;

				try {
					connection = jdbcUtil.getConnection();
					String sql = "insert into student(name,password) values(？，？);";
					preparedStatement = connection.prepareStatement(sql);
					preparedStatement.setString(1, username);
					preparedStatement.setString(2, userpassword);
				
					count = preparedStatement.executeUpdate();
					if (count == 1) {
						JOptionPane.showMessageDialog(null, "注册成功");

					} else {
						JOptionPane.showMessageDialog(null, "用户名已存在");

					}

				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if (preparedStatement != null) {
					try {
						preparedStatement.cancel();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				if (connection != null) {
					try {
						connection.close();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
		// 事件监听 登录按钮
		jButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String username = jTextField.getText();
				String userpassword = jTextField1.getText();
				System.out.println(username);
				System.out.println(userpassword);
				Connection connection = null;
				PreparedStatement preparedStatement = null;
				ResultSet resultSet = null;
				try {
					connection = jdbcUtil.getConnection();
					String sql = "select * from student where name=? and password=?";
					preparedStatement = connection.prepareStatement(sql);
					preparedStatement.setString(1, username);
					preparedStatement.setString(2, userpassword);
					
					resultSet = preparedStatement.executeQuery();

					if (resultSet.next()) {

						System.out.println("登录成功");
						MainFrame mainFrame = new MainFrame();
						mainFrame.init();
					} else {
						JOptionPane.showMessageDialog(null, "用户名或密码错误");
					}

				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} finally {
					if (resultSet != null) {
						try {
							resultSet.close();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					if (preparedStatement != null) {
						try {
							preparedStatement.cancel();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					if (connection != null) {
						try {
							connection.close();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}
			}
		});

		jFrame.setVisible(true);
		jFrame.setSize(200, 350);
		jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE);
	}

}
